package com.shuoyue.ycgk.ui.questionbank.practice.freedom;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.shuoyue.ycgk.entity.FreedomGroup;
import com.shuoyue.ycgk.entity.QuestionParent;
import com.shuoyue.ycgk.entity.QuestionSimple;
import com.shuoyue.ycgk.entity.UploadEstimateSucInfo;
import com.shuoyue.ycgk.entity.UserAnswerPager;
import com.shuoyue.ycgk.ui.estimate.EsSourtActivity;
import com.shuoyue.ycgk.ui.questionbank.QuestionUtil;
import com.shuoyue.ycgk.ui.questionbank.UploadAnswerContract;
import com.shuoyue.ycgk.ui.questionbank.base.BaseQuestionActivity;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.AnswerCardDialog;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.ArrayList;
import java.util.List;

import static com.shuoyue.ycgk.ui.questionbank.QuestionUtil.getUserAnswerPager;

public class FreedomQuestionActivity extends BaseQuestionActivity
        implements FreedomContract.View, UploadAnswerContract.View {
    String pagerName;
    ArrayList<FreedomGroup> freedomGroups;
    FreedomContract.Presenter freedomPresenter;
    int testId;
    UploadAnswerContract.Presenter anserPresenter;
    boolean isUploadAnswer = false;
    public boolean isShowRight = true;

    public static void start(Context mContext, int userTestId, String name, boolean isShowRight) {
        mContext.startActivity(new Intent(mContext, FreedomQuestionActivity.class)
                .putExtra("id", userTestId)
                .putExtra("isShowRight", isShowRight)

                .putExtra("name", name));
    }

    @Override
    protected void initView() {
        super.initView();
        card.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        super.initData();
        testId = getIntent().getIntExtra("id", -1);
        pagerName = getIntent().getStringExtra("name");
        isShowRight = getIntent().getBooleanExtra("isShowRight", true);

        freedomPresenter = new FreedomContract.Presenter();
        freedomPresenter.attachView(this);
        anserPresenter = new UploadAnswerContract.Presenter();
        anserPresenter.attachView(this);
        freedomPresenter.getFreedomQuestion(testId);
    }

    @Override
    public Class getClassT() {
        return FreedomMateralFragment.class;
    }

    @Override
    public Class getClassK() {
        return FreedomSimpleFragment.class;
    }

    @Override
    public void lastOne() {
        FreedomCountActivity.start(mContext, pagerName, testId, freedomGroups);
        super.finish();
    }

    @Override
    public void back() {
        finish();
    }


    @Override
    public void getQuestionsSuc(List<FreedomGroup> freedomGroups) {
        this.freedomGroups = (ArrayList<FreedomGroup>) freedomGroups;
        if (freedomGroups.size() == 1) {
            setThisActData(freedomGroups.get(0).getQuestionDTOS(), isShowRight);
        } else {
            List<QuestionParent> questionParents = new ArrayList<>();

            for (int i = 1; i <= freedomGroups.size(); i++) {
                FreedomGroup freedomGroup = freedomGroups.get(i - 1);
                if (freedomGroup.getQuestionDTOS() == null) {
                    continue;
                }
                QuestionParent questionParent = new QuestionParent();
                questionParent.setSpcePage(true);
                questionParent.setNowGroupPos(i);
                questionParent.setSpaceName(freedomGroup.getDetailName());
                questionParent.setTotalGroup(freedomGroups.size());
                questionParents.add(questionParent);
                questionParents.addAll(freedomGroup.getQuestionDTOS());
            }
            setThisActData(questionParents, isShowRight);
        }

    }

    @Override
    public void getFreedomSuc(List<QuestionParent> freedoms) {
        setThisActData(freedoms);
    }

    @Override
    public void finish() {
        HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext,
                "提示",
                "是否退出");
        hintConfirmDialog.setOkListener(v -> {
            UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, testId, null);
            if (userAnswerPager == null) {
                super.finish();
            } else {
                anserPresenter.uploadAnswerNoView(userAnswerPager, 5, true);
                super.finish();
            }

        });
        hintConfirmDialog.show();
    }

    @Override
    public void uploadSuc(boolean isNext) {
        isUploadAnswer = true;
        XToast.toast(mContext, "答案已提交");
        if (isNext) {
            super.finish();
        } else {
            FreedomCountActivity.start(mContext, pagerName, testId, freedomGroups);
            FreedomQuestionActivity.super.finish();
        }
    }

    @Override
    public void uploadEstimatteSuc(boolean b, UploadEstimateSucInfo uploadEstimateSucInfo, int submit) {

    }

    @Override
    public void uploadFail(boolean isNext, int submit) {

    }


    AnswerCardDialog answerCardDialog;

    @Override
    public void showAnswerCard() {
        if (answerCardDialog == null) {
            answerCardDialog = new AnswerCardDialog(mContext, questionParents);
            answerCardDialog.addSelectCall(new AnswerCardDialog.AnswerCardCallBack() {
                @Override
                public void select(QuestionSimple questionSimple) {
                    goDesPosition(questionSimple);
                }

                @Override
                public void submit() {
                    UserAnswerPager userAnswerPager = QuestionUtil.getUserAnswerPager(questionParents, testId, null);
                    if (userAnswerPager == null) {
                        XToast.toast(mContext, "没有答案可以提交");
                    } else {
//                        userAnswerPager.setTime(useTimeMul / 1000);
                        userAnswerPager.setIsSubmit(1);
                        anserPresenter.uploadAnswer(userAnswerPager, 5, false);
                    }

//                    FreedomCountActivity.start(mContext, pagerName, testId, freedomGroups);
//                    FreedomQuestionActivity.super.finish();
                }
            });
        }
        answerCardDialog.setButtonText("提交试卷");
        answerCardDialog.show();
    }

}
